// pages/setInfo/setInfo.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';
const app = getApp();
const db = wx.cloud.database();
var openid = app.globalData.openid;
var avatar = "";
var nickname = "";
var signature = "";
var first = false;
var info = {"avatar":"","nickname":"","signature": ""};
Page({

    /**
     * 页面的初始数据
     */
    data: {
        avatarUrl: defaultAvatarUrl,
        loginSuccessful: false
    },

    onChooseAvatar(e) {
        const { avatarUrl } = e.detail;
        info["avatar"] = e.detail["avatarUrl"];
        avatar = e.detail["avatarUrl"];
        this.setData({
            avatarUrl,
        })
    },
    getText(e){
        nickname = e.detail.value;
        info["nickname"] = nickname;
    },
    getSignatureText(e){
        signature = e.detail.value;
        info["signature"] = signature;
    },
    saveError(){
        let that = this;
        let animation = wx.createAnimation({
            duration: 50,
            timingFunction: 'ease',
        });
        animation.translateX(-25).step()
        this.setData({
            errorAni:  animation.export()
        })
        setTimeout(function(){
            animation = wx.createAnimation({
                duration: 50,
                timingFunction: 'ease',
            });
            animation.translateX(+25).step()
            that.setData({
                errorAni:  animation.export()
            });
            setTimeout(function(){
                animation = wx.createAnimation({
                    duration: 50,
                    timingFunction: 'ease',
                });
                animation.translateX(0).step()
                that.setData({
                    errorAni:  animation.export()
                });
            },50);
        },50);
        //按钮抖动
    },
    saveInfo(){
        let that = this;
        let cloudPath = "avatar/" + openid + '.jpg';
        if ( nickname != undefined && avatar != undefined ) {
            wx.showLoading({
                title: '保存中...',
            });

            wx.cloud.uploadFile({
                filePath: avatar,
                cloudPath: cloudPath,
                success: (res) => {
                    let cloudAvatar = res.fileID;
                    db.collection("infoDB").where({
                        _openid: app.globalData.openid
                    }).update({
                        data: {
                            avatar: cloudAvatar,
                            nickname: nickname,
                            signature: signature
                        },
                        success: function(res) {
                            wx.hideLoading();
                            wx.navigateBack({ delta: 0 });
                        }
                    });
                }
            });
        }
        else {
            wx.showToast({
              title: '没有设置头像或昵称！',
              icon: 'none'
            })
            this.saveError();
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this;
        openid = app.globalData.openid;
        if ( !openid ) {
            wx.cloud.callFunction({ //  调用云函数获取openid
                name: 'getopenid',
                complete: res => {
                    openid = res.result.openid;
                    app.globalData.openid = openid; 
                }
            });
        }
        else {
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.on('acceptDataFromOpenerPage', function(data) {
                avatar = data.avatar;
                nickname = data.nickname;
                signature = data.signature;
                openid = app.globalData.openid;
                first =  data.first;
                that.setData({
                    avatarUrl: avatar,
                    nickName: nickname,
                    signature: signature
                });
            });
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})